package com.ldl.distributelock.controller;

import com.ldl.distributelock.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StockController {

    @Autowired
    private StockService stockService;

    @GetMapping("stock/deduct")
    public String deduct(){
        stockService.deduct();
        return "hello stock deduce!!";
    }

    @GetMapping("test/fair/lock/{id}")
    public String testFairLock(@PathVariable("id")Long id){
        this.stockService.testFairLock(id);
        return "hello test fair lock !!";
    }

    @GetMapping("test/read/lock")
    public String testReadLock() throws InterruptedException {
        this.stockService.testReadLock();
        return "hello test read lock !!";
    }

    @GetMapping("test/write/lock")
    public String testWriteLock() throws InterruptedException {
        this.stockService.testWriteLock();
        return "hello test write lock !!";
    }

    @GetMapping("test/semaphore")
    public String testSemaphore() throws InterruptedException {
        this.stockService.testSemaphore();
        return "hello Semaphore";
    }
}
